class Conf < ActiveRecord::Base

  DEFAULT_PORT	= 6718 # flix!
  LOG_PATH			= RAILS_ROOT + '/log/netfluxd.log'

  def self.is_valid
    fields = {
      'nfSignIn'      => 'Netflix sign-in',
      'nfPassword'    => 'Netflix password',
      'PollInterval'  => 'poll interval',
      'Path'          => 'video folder'
    }

    result = []

    fields.each{ |field, desc|
      value = self[field]
      if not value or value.empty?
        result.push(desc)
      end
    }

    [result.empty?, result]
  end

  def self.Port
    p = self.[]('Port')

    (p and not p.empty?) ? p : DEFAULT_PORT
  end

  def self.[](name)
    row = self.find_by_name(name)
    row ? row.value : nil
  end

  def self.[]=(name, value)
    row = self.find_or_initialize_by_name(name)
    if value
      row.value = value
      row.save
    else
      row.destroy if not row.new_record?
    end
  end

  def self.method_missing(symbol, *args)
    begin
      super(symbol, *args)
    rescue
      name = symbol.id2name

      if args.empty?
        self[name]
      else
        self[name.chomp('=')] = *args
      end
    end
  end

end
